<?php
	require_once("htons-ntohs.php");
	require_once("protocoldef.php");

	//error_reporting(0);
	
	// 开始时间
	$start=microtime(true);

	// 数据库前缀、表名、字段名
	///////////////           注意        /////////////////////////////////////////////////////////////////////////////////////////
	///// field字段名称的顺序，一定要按照【msg_id】、【create_time】、【user_id】的顺序进行填写，不能错乱 /////////////////////////////
	$DBNAME=array(
		0 => array(
			"dbname" => "db2_mi_",
			"dbnum"  => 1,
			"tbname" => "t_mi_",
			"tbnum"  =>  1,
			"field"  => array(		
				0 => "msg_id",
				1 => "create_time",
				2 => "cmt_id"
			)
		)
	);
/*	
	$DBNAME=array(
		0 => array(
			"dbname" => "db2_si_",
			"dbnum"  => 10,
			"tbname" => "t_si_",
			"tbnum"  =>  1,
			"field"  => array(		
				0 => "user_id",
				1 => "msg_id",
				2 => "create_time"
			)
		),
	);
*/	
/*	
	$DBNAME=array(
		0 => array(
			"dbname" => "db2_cfi_",
			"dbnum"  => 10,
			"tbname" => "t_cfi_",
			"tbnum"  =>  1,
			"field"  => array(		
				0 => "user_id",
				1 => "msg_id",
				2 => "create_time"
			)
		),
		1 => array(
			"dbname" => "db2_pici_",
			"dbnum"  => 10,
			"tbname" => "t_pici_",
			"tbnum"  => 1,
			"field"  => array(
				0 => "photo_id",
				1 => "cmt_id",
				2 => "create_time"
			)
		),
		2 => array(
			"dbname" => "db2_ci_",
			"dbnum"  => 10,
			"tbname" => "t_ci_",
			"tbnum"  => 1,
			"field"  => array(
				0 => "m_cmt_id",
				1 => "cmt_id",
				2 => "create_time"
			)
		)
	);
*/
	

	//define("DBNAME","db2_si_0_");
	//define("TBNAME","t_si_0");
	define("MONGODB","10.245.75.74");
	define("MONGOPORT",65431);

	//define("INDEXDB","10.245.75.75");
	//define("INDEXPORT",58001);
	define("INDEXDB","10.245.66.21");
	define("INDEXPORT",58000);
	// 数据包长度
	define("PACKLENGTH",37);
	// 重试次数
	define("RETRY",5);

	// 初始化头
	$uvheader=new UVHEADER();
	$uvheader->header["len"]["value"]	= PACKLENGTH;
	$uvheader->header["cmd"]["value"]	= 3;
	uvpack($uvheader->header);

	// 预先声明InsertCmd、RspdInsertCmd对象
	$insertcmd	= new InsertCmd();
	// 貌似可以不要
	//$rspdinsertcmd	= new RspdInsertCmd();

	// 连接Mongo
	$server="mongodb://".MONGODB.":".MONGOPORT;
	//$conn=new Mongo("mongodb://10.245.75.74:65431");
	$conn=new Mongo($server);
	if($conn===false)
	{
		print "Mongo() fail<br />";
		exit(-1);
	}

	// 与索引系统建立socket长连接
	$sockfd=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
	if($sockfd==false)
	{
		printf("socket_create fail.[%d-%s]<br />",socket_last_error(),socket_strerror(socket_last_error()));
		exit(-1);
	}

	if((socket_connect($sockfd,INDEXDB,INDEXPORT))==false)
	{
		printf("socket_connect fail.[%d-%s]<br />",socket_last_error(),socket_strerror(socket_last_error()));
		exit(-1);
	}


	$totalrecord=1;
	foreach($DBNAME as $dbkey => $dbvalue)
	{
		$dbnum=$DBNAME[$dbkey]['dbnum'];
		for($i=0;$i<$dbnum;$i++)
		{
			$dbname = $DBNAME[$dbkey]['dbname']."0_".$i;

			$db=$conn->selectDB($dbname);
			printf("dbname = %s dbnum = %d start:\n",$dbname,$i);

			// 获取三个索引字段的字段名称
			$key1	= $DBNAME[$dbkey]['field'][0];
			$key2	= $DBNAME[$dbkey]['field'][1];
			$key3	= $DBNAME[$dbkey]['field'][2];

			$tbnum=$DBNAME[$dbkey]['tbnum'];
			for($j=0;$j<$tbnum;$j++)
			{
				$tbname = $DBNAME[$dbkey]['tbname'].$j;

				$collect=$db->selectCollection($tbname);
				printf("tbname = %s tbnum = %d start:\n",$tbname,$j);
				
				$cursor=$collect->find();
				$num=1;
				foreach($cursor as $obj)
				{

					/////////////////// 此处的顺序一定不能错乱 /////////////////
					///// field字段名称的顺序，一定要按照【msg_id】、【create_time】、【user_id】的顺序进行填写，不能错乱 /////////////////////////////
					$insertcmd->cmd["msg_id"]["value"]		= $obj[$key1];
					$insertcmd->cmd["create_time"]["value"]		= $obj[$key2];
					$insertcmd->cmd["user_id"]["value"]		= $obj[$key3];
					printf("totalrecord = %u current num = %u %s = %lu %s = %u %s = %lu\n",$totalrecord,$num,$key1,$obj[$key1],$key2,$obj[$key2],$key3,$obj[$key3]);
					$num++;
					$totalrecord++;

					uvpack($insertcmd->cmd);

					// 头部格式及参数值
					$FORMAT	=	"CSSSC";
					$PARA[0]=	$uvheader->header["begin"]["value"];
					$PARA[1]=	$uvheader->header["len"]["value"];
					$PARA[2]=	$uvheader->header["ver"]["value"];
					$PARA[3]=	$uvheader->header["cmd"]["value"];
					$PARA[4]=	$uvheader->header["reserve"]["value"];
				
					// 判断列是否64位，如果是，则需要进行换位
					$flag		= 0;
					$addafter32	= "";
					$addbefore32	= "";
					foreach($insertcmd->cmd as $key => $value)
					{
						if(($key=="msg_id")||($key=="create_time")||($key=="user_id"))
						{
							$idtype=$insertcmd->cmd[$key]["type"];
							if($uvbyte[$idtype]==8)
							{	
								$temp1=decbin($insertcmd->cmd[$key]["value"]);
								$before32	= substr($temp1,0,-32);
								$after32	= substr($temp1,-32);
								$before32	= bindec($before32);
								$after32	= bindec($after32);
								$PARA[]		= $after32;
								$PARA[]		= $before32;
								$FORMAT		= $FORMAT."LL";	
								// 判断是否需要重复
								if($insertcmd->cmd[$key]["repeat"]==1)
								{
									$flag=1;
									$addafter32	= $after32;
									$addbefore32	= $before32;
								}
							}
							else
							{
								$PARA[]		= $insertcmd->cmd[$key]["value"];
								$FORMAT		= $FORMAT."L";	
							}
						}
					}

					if($flag==1)
					{
						$PARA[]	= $addafter32;
						$PARA[]	= $addbefore32;
						$FORMAT	= $FORMAT."LL";
					}

					
					$PARA[]	= $insertcmd->cmd["end"]["value"];
					$FORMAT	= $FORMAT."C";
					$temp=call_user_func_array("pack",array_merge((array)$FORMAT,$PARA));

					for($n = 0;$n < PACKLENGTH; $n++)
					//for($n = 0;$n < 29; $n++)
					{
						printf("%02x ",ord($temp[$n]));
						if(($n!=0)&&(($n+1)%8==0))
							printf("\n");

					}

					printf("\n");

		
					$buflen=PACKLENGTH;
					$retry=1;
					$ret=0;
					while($retry<=RETRY)
					{
						$ret=socket_send($sockfd,$temp,$buflen,NULL);
						//$ret=socket_write($sockfd,$temp,$buflen);
						if($ret==$buflen)
						{
							break;
						}

						$retry++;
					}

					if($retry>RETRY)
					{
						printf("send data fail,sockfd = %s,totalrecord = %u,[%d-%s]\n",$sockfd,$totalrecord,socket_last_error(),socket_strerror(socket_last_error()));
						continue;
					}


					$temp="";
					$ret=socket_recv($sockfd,$temp,13,NULL);
					if(($ret===false)||($ret!==13))
					{
						printf("recv data fail,sockfd = %s,totalrecord = %u,[%d-%s]\n",$sockfd,$totalrecord,socket_last_error(),socket_strerror(socket_last_error()));
						continue;
					}

					$temp1=unpack("C/n/n/n/C/Ncode/C",$temp);
					if($temp["code"]!=0)
					{
						printf("unpack data fail,sockfd = %s,totalrecord = %u,[%d-%s]\n",$sockfd,$totalrecord,socket_last_error(),socket_strerror(socket_last_error()));
						continue;
					}	

					//if($totalrecord>=3)
					//	exit;

					unset($PARA);
					unset($FORMAT);

				} // foreach($cursor as $obj)
			} // for($j=0;$j<$tbnum;$j++)
			printf("\n");
		} // for($i=0;$i<$dbnum;$i++)
	} // foreach($DBNAME as $dbkey => $dbvalue)
	
	socket_close($sockfd);
	$conn->close();

	echo "TOTAL TIME : ".(microtime(true)-$start)."\n\n";
?>
